package com.zhoug.ocr.textin.parser;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.ocr.textin.data.Product;
import com.zhoug.ocr.textin.data.VatInvoiceResult;
import com.zhoug.ocr.textin.data.bill.VatInvoice;

import java.util.ArrayList;
import java.util.List;

/**
 * 增值税发票结果格式化
 * @Author: zhoug
 * @Date: 2023/3/3
 * @Description:
 */
public class VatInvoiceResultParser {
    private static final String TAG = ">>>VatInvoiceResultParser";

    public VatInvoice parseVatInvoice(VatInvoiceResult result){
        LogUtils.d(TAG, "parseVatInvoice:"+result);
        VatInvoice vatInvoice = null;
        if (result != null) {
            List<VatInvoiceResult> object_list = result.getObject_list();
            if (object_list != null && object_list.size() > 0) {
                result = object_list.get(0);
            }
            vatInvoice = new VatInvoice();
            List<VatInvoiceResult.Item> item_list = result.getItem_list();
            vatInvoice.setInvoiceType(result.getType_description());//发票类型
            vatInvoice.setInvoiceName("");//发票名称
            if (item_list != null && item_list.size() > 0) {
                for (VatInvoiceResult.Item item : item_list) {
                    String key = item.getKey();
                    String value = item.getValue();
                    if ("vat_invoice_daima".equalsIgnoreCase(key)) {
                        vatInvoice.setInvoiceCode(value);//发票代码
                    } else if ("vat_invoice_haoma".equalsIgnoreCase(key)) {
                        vatInvoice.setInvoiceNo(value);//发票号码
                    } else if ("vat_invoice_issue_date".equalsIgnoreCase(key)) {
                        vatInvoice.setInvoiceData(value);//开票日期
                    } else if ("vat_invoice_correct_code".equalsIgnoreCase(key)) {
                        vatInvoice.setCheckCode(value);//校验码
                    } else if ("vat_invoice_machine_no".equalsIgnoreCase(key)) {
                        vatInvoice.setMachineCode(value);//机器编号
                    } else if ("vat_invoice_cipher_field".equalsIgnoreCase(key)) {
                        vatInvoice.setPassword(value);//密码区
                    } else if ("vat_invoice_total_cover_tax_digits".equalsIgnoreCase(key)) {
                        vatInvoice.setAmountInFiguers(value);//价税合计(小写)
                    } else if ("vat_invoice_total_cover_tax".equalsIgnoreCase(key)) {
                        vatInvoice.setAmountInWords(value);//价税合计(大写)
                    } else if ("vat_invoice_total".equalsIgnoreCase(key)) {
                        vatInvoice.setTotalAmount(value);//合计金额
                    } else if ("vat_invoice_tax_total".equalsIgnoreCase(key)) {
                        vatInvoice.setTotalTax(value);//合计税额
                    } else if ("vat_invoice_payer_name".equalsIgnoreCase(key)) {
                        vatInvoice.setPurchaserName(value);//购买方名称
                    } else if ("vat_invoice_rate_payer_id".equalsIgnoreCase(key)) {
                        vatInvoice.setPurchaserRegisterNum(value);//购买方纳税人识别号
                    } else if ("vat_invoice_payer_addr_tell".equalsIgnoreCase(key)) {
                        vatInvoice.setPurchaserAddressAndPhone(value);//购买方地址、电话
                    } else if ("vat_invoice_payer_bank_account".equalsIgnoreCase(key)) {
                        vatInvoice.setPurchaserBank(value);//购买方开户行及账号
                    } else if ("vat_invoice_seller_name".equalsIgnoreCase(key)) {
                        vatInvoice.setSellerName(value);//销售方名称
                    } else if ("vat_invoice_seller_id".equalsIgnoreCase(key)) {
                        vatInvoice.setSellerRegisterNum(value);//销售方纳税人识别号
                    } else if ("vat_invoice_seller_addr_tell".equalsIgnoreCase(key)) {
                        vatInvoice.setSellerAddressAndPhone(value);//销售方地址、电话
                    } else if ("vat_invoice_seller_bank_account".equalsIgnoreCase(key)) {
                        vatInvoice.setSellerBank(value);//销售方开户行及账号
                    } else if ("vat_invoice_payee".equalsIgnoreCase(key)) {
                        vatInvoice.setPayee(value);//收款人
                    } else if ("vat_invoice_review".equalsIgnoreCase(key)) {
                        vatInvoice.setChecker(value);//复核
                    } else if ("vat_invoice_drawer".equalsIgnoreCase(key)) {
                        vatInvoice.setNoteDrawer(value);//开票人
                    } else if ("vat_invoice_total_note".equalsIgnoreCase(key)) {
                        vatInvoice.setRemarks(value);//备注
                    }

                }
            }

            List<List<VatInvoiceResult.Product>> product_list = result.getProduct_list();
            if (null != product_list && product_list.size() > 0) {
                List<Product> products = new ArrayList<>();
                vatInvoice.setProducts(products);
                for (int i = 0; i < product_list.size(); i++) {
                    List<VatInvoiceResult.Product> items = product_list.get(i);
                    if (items != null && items.size() > 0) {
                        Product product = new Product();
                        products.add(product);
                        for (VatInvoiceResult.Product item : items) {
                            String key = item.getKey();
                            String value = item.getValue();
                            if ("vat_invoice_goods".equalsIgnoreCase(key)) {
                                product.setProductName(value);
                            } else if ("vat_invoice_plate_specific".equalsIgnoreCase(key)) {
                                product.setSpecification(value);
                            } else if ("vat_invoice_electrans_unit".equalsIgnoreCase(key)) {
                                product.setUnit(value);
                            } else if ("vat_invoice_electrans_quantity".equalsIgnoreCase(key)) {
                                product.setNum(value);
                            } else if ("vat_invoice_electrans_unit_price".equalsIgnoreCase(key)) {
                                product.setPrice(value);
                            } else if ("vat_invoice_price".equalsIgnoreCase(key)) {
                                product.setPriceSum(value);
                            } else if ("vat_invoice_tax_rate".equalsIgnoreCase(key)) {
                                product.setTaxRate(value);
                            } else if ("vat_invoice_tax".equalsIgnoreCase(key)) {
                                product.setTaxAmount(value);
                            }
                        }
                    }
                }
            }

        }
        return vatInvoice;
    }
}
